/*
 * This class holds the information about the CD player which is the mediator of the doomain
 */
package edu.iyte.ceng431.hw03.cdplayer.model;

import java.util.Observer;

/**
 *
 * @author hp
 */
public class CDPlayer {

    private PlayingCD cd;
    private Drawer drawer;

    public CDPlayer() {
        cd = null;
        drawer = new Drawer();
    }

    //loads the CD to the CD player
    public void load() {
        stop();
        if (cd != null) {
            cd.setIndex(0);
        }
        drawer.load();
    }

    //if the drawer is open, first closes the drawer and plays the current track
    public void playPause() {
        if (drawer.isOpen()) {
            drawer.load();
        }
        if (cd != null) {
            cd.playPause();
        }
    }

    //proceed to the previous track
    public void previousTrack() {
        if (drawer.isOpen()) {
            drawer.load();
        }
        if (cd != null) {
            cd.previousTrack();
        }
    }

    //proceed to the next track
    public void nextTrack() {
        if (drawer.isOpen()) {
            drawer.load();
        }
        if (cd != null) {
            cd.nextTrack();
        }
    }

    //stop the current track
    public void stop() {
        if (cd != null) {
            cd.stop();
        }
    }

    /**
     * @return the cd
     */
    public CD getCd() {
        return cd;
    }

    /**
     * @param cd the cd to set
     */
    public void setCd(PlayingCD cd) {
        this.cd = cd;
        stop();
    }

    public void addObservers(Observer view) {
        drawer.addObserver(view);
    }
}
